﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
using System.Collections.Generic;

namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cmsArticle。
    /// </summary>
    public class cmsArticle
    {
        public cmsArticle()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ArtId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cmsArticle");
            strSql.Append(" where ArtId=" + ArtId + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cmsArticle model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.TypeId != null)
            {
                strSql1.Append("TypeId,");
                strSql2.Append("" + model.TypeId + ",");
            }
            if (model.BorderId != null)
            {
                strSql1.Append("BorderId,");
                strSql2.Append("" + model.BorderId + ",");
            }
            if (model.ParentArtId != null)
            {
                strSql1.Append("ParentArtId,");
                strSql2.Append("" + model.ParentArtId + ",");
            }
            if (model.ArtOrder != null)
            {
                strSql1.Append("ArtOrder,");
                strSql2.Append("" + model.ArtOrder + ",");
            }
            if (model.ArtDirectory != null)
            {
                strSql1.Append("ArtDirectory,");
                strSql2.Append("'" + model.ArtDirectory + "',");
            }
            if (model.ArtTopic != null)
            {
                strSql1.Append("ArtTopic,");
                strSql2.Append("'" + model.ArtTopic + "',");
            }
            if (model.ArtKeys != null)
            {
                strSql1.Append("ArtKeys,");
                strSql2.Append("'" + model.ArtKeys + "',");
            }
            if (model.ArtFrom != null)
            {
                strSql1.Append("ArtFrom,");
                strSql2.Append("'" + model.ArtFrom + "',");
            }
            if (model.ArtSummary != null)
            {
                strSql1.Append("ArtSummary,");
                strSql2.Append("'" + model.ArtSummary + "',");
            }
            if (model.ArtFileName != null)
            {
                strSql1.Append("ArtFileName,");
                strSql2.Append("'" + model.ArtFileName + "',");
            }
            if (model.ModfiyTime != null)
            {
                strSql1.Append("ModfiyTime,");
                strSql2.Append("'" + model.ModfiyTime + "',");
            }
            if (model.hits != null)
            {
                strSql1.Append("hits,");
                strSql2.Append("" + model.hits + ",");
            }
            if (model.Modify != null)
            {
                strSql1.Append("Modify,");
                strSql2.Append("" + model.Modify + ",");
            }
            if (model.Writer != null)
            {
                strSql1.Append("Writer,");
                strSql2.Append("" + model.Writer + ",");
            }
            if (model.Auditer != null)
            {
                strSql1.Append("Auditer,");
                strSql2.Append("'" + model.Auditer + "',");
            }
            if (model.Audit != null)
            {
                strSql1.Append("Audit,");
                strSql2.Append("" + (model.Audit ? 1 : 0) + ",");
            }
            if (model.ArtFormat != null)
            {
                strSql1.Append("ArtFormat,");
                strSql2.Append("" + model.ArtFormat + ",");
            }
            if (model.Version != null)
            {
                strSql1.Append("Version,");
                strSql2.Append("" + model.Version + ",");
            }
            if (model.CanView != null)
            {
                strSql1.Append("CanView,");
                strSql2.Append("" + (model.CanView ? 1 : 0) + ",");
            }
            if (model.ArtViewPurview != null)
            {
                strSql1.Append("ArtViewPurview,");
                strSql2.Append("'" + model.ArtViewPurview + "',");
            }
            if (model.ArtEditPurview != null)
            {
                strSql1.Append("ArtEditPurview,");
                strSql2.Append("'" + model.ArtEditPurview + "',");
            }
        
            if (model.ShowOrder != null)
            {
                strSql1.Append("ShowOrder,");
                strSql2.Append("" + (model.ShowOrder ? 1 : 0) + ",");
            }
            if (model.IsVersion != null)
            {
                strSql1.Append("IsVersion,");
                strSql2.Append("" + (model.IsVersion ? 1 : 0) + ",");
            }
            if (model.IsTop != null)
            {
                strSql1.Append("IsTop,");
                strSql2.Append("'" + model.IsTop + "',");
            }
            if (model.PublishStatus != null)
            {
                strSql1.Append("PublishStatus,");
                strSql2.Append("" + model.PublishStatus + ",");
            }
            strSql.Append("insert into cmsArticle(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cmsArticle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cmsArticle set ");
            if (model.TypeId != null)
            {
                strSql.Append("TypeId=" + model.TypeId + ",");
            }
            if (model.BorderId != null)
            {
                strSql.Append("BorderId=" + model.BorderId + ",");
            }
            if (model.ParentArtId != null)
            {
                strSql.Append("ParentArtId=" + model.ParentArtId + ",");
            }
            if (model.ArtOrder != null)
            {
                strSql.Append("ArtOrder=" + model.ArtOrder + ",");
            }
            if (model.ArtDirectory != null)
            {
                strSql.Append("ArtDirectory='" + model.ArtDirectory + "',");
            }
            if (model.ArtTopic != null)
            {
                strSql.Append("ArtTopic='" + model.ArtTopic + "',");
            }
            if (model.ArtKeys != null)
            {
                strSql.Append("ArtKeys='" + model.ArtKeys + "',");
            }
            if (model.ArtFrom != null)
            {
                strSql.Append("ArtFrom='" + model.ArtFrom + "',");
            }
            if (model.ArtSummary != null)
            {
                strSql.Append("ArtSummary='" + model.ArtSummary + "',");
            }
            if (model.ArtFileName != null)
            {
                strSql.Append("ArtFileName='" + model.ArtFileName + "',");
            }
            if (model.ModfiyTime != null)
            {
                strSql.Append("ModfiyTime='" + model.ModfiyTime + "',");
            }
            if (model.hits != null)
            {
                strSql.Append("hits=" + model.hits + ",");
            }
            if (model.Modify != null)
            {
                strSql.Append("Modify=" + model.Modify + ",");
            }
            if (model.Writer != null)
            {
                strSql.Append("Writer=" + model.Writer + ",");
            }
            if (model.Auditer != null)
            {
                strSql.Append("Auditer='" + model.Auditer + "',");
            }
            if (model.Audit != null)
            {
                strSql.Append("Audit=" + (model.Audit ? 1 : 0) + ",");
            }
            if (model.ArtFormat != null)
            {
                strSql.Append("ArtFormat=" + model.ArtFormat + ",");
            }
            if (model.Version != null)
            {
                strSql.Append("Version=" + model.Version + ",");
            }
            if (model.CanView != null)
            {
                strSql.Append("CanView=" + (model.CanView ? 1 : 0) + ",");
            }
            if (model.ArtViewPurview != null)
            {
                strSql.Append("ArtViewPurview='" + model.ArtViewPurview + "',");
            }
            if (model.ArtEditPurview != null)
            {
                strSql.Append("ArtEditPurview='" + model.ArtEditPurview + "',");
            }
        
            if (model.ShowOrder != null)
            {
                strSql.Append("ShowOrder=" + (model.ShowOrder ? 1 : 0) + ",");
            }
            if (model.IsVersion != null)
            {
                strSql.Append("IsVersion=" + (model.IsVersion ? 1 : 0) + ",");
            }
            if (model.IsTop != null)
            {
                strSql.Append("IsTop='" + model.IsTop + "',");
            }
            if (model.PublishStatus != null)
            {
                strSql.Append("PublishStatus=" + model.PublishStatus + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ArtId=" + model.ArtId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(long ArtId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cmsArticle ");
            strSql.Append(" where ArtId=" + ArtId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cmsArticle GetModel(long ArtId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ArtId,TypeId,BorderId,ParentArtId,ArtOrder,ArtDirectory,ArtTopic,ArtKeys,ArtFrom,ArtSummary,ArtFileName,ModfiyTime,hits,Modify,Writer,Auditer,Audit,ArtFormat,Version,CanView,ArtViewPurview,ArtEditPurview,ShowOrder,IsVersion,IsTop,PublishStatus ");
            strSql.Append(" from cmsArticle ");
            strSql.Append(" where ArtId=" + ArtId + " ");
            Usas.Model.Cms.cmsArticle model = new Usas.Model.Cms.cmsArticle();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ArtId"].ToString() != "")
                {
                    model.ArtId = long.Parse(ds.Tables[0].Rows[0]["ArtId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeId"].ToString() != "")
                {
                    model.TypeId = int.Parse(ds.Tables[0].Rows[0]["TypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BorderId"].ToString() != "")
                {
                    model.BorderId = int.Parse(ds.Tables[0].Rows[0]["BorderId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentArtId"].ToString() != "")
                {
                    model.ParentArtId = long.Parse(ds.Tables[0].Rows[0]["ParentArtId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ArtOrder"].ToString() != "")
                {
                    model.ArtOrder = long.Parse(ds.Tables[0].Rows[0]["ArtOrder"].ToString());
                }
                model.ArtDirectory = ds.Tables[0].Rows[0]["ArtDirectory"].ToString();
                model.ArtTopic = ds.Tables[0].Rows[0]["ArtTopic"].ToString();
                model.ArtKeys = ds.Tables[0].Rows[0]["ArtKeys"].ToString();
                model.ArtFrom = ds.Tables[0].Rows[0]["ArtFrom"].ToString();
                model.ArtSummary = ds.Tables[0].Rows[0]["ArtSummary"].ToString();
                model.ArtFileName = ds.Tables[0].Rows[0]["ArtFileName"].ToString();
                if (ds.Tables[0].Rows[0]["ModfiyTime"].ToString() != "")
                {
                    model.ModfiyTime = DateTime.Parse(ds.Tables[0].Rows[0]["ModfiyTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits"].ToString() != "")
                {
                    model.hits = int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modify"].ToString() != "")
                {
                    model.Modify = int.Parse(ds.Tables[0].Rows[0]["Modify"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Writer"].ToString() != "")
                {
                    model.Writer = int.Parse(ds.Tables[0].Rows[0]["Writer"].ToString());
                }
                model.Auditer = ds.Tables[0].Rows[0]["Auditer"].ToString();
                if (ds.Tables[0].Rows[0]["Audit"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Audit"].ToString() == "1") || (ds.Tables[0].Rows[0]["Audit"].ToString().ToLower() == "true"))
                    {
                        model.Audit = true;
                    }
                    else
                    {
                        model.Audit = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ArtFormat"].ToString() != "")
                {
                    model.ArtFormat = int.Parse(ds.Tables[0].Rows[0]["ArtFormat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Version"].ToString() != "")
                {
                    model.Version = decimal.Parse(ds.Tables[0].Rows[0]["Version"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CanView"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["CanView"].ToString() == "1") || (ds.Tables[0].Rows[0]["CanView"].ToString().ToLower() == "true"))
                    {
                        model.CanView = true;
                    }
                    else
                    {
                        model.CanView = false;
                    }
                }
                model.ArtViewPurview = ds.Tables[0].Rows[0]["ArtViewPurview"].ToString();
                model.ArtEditPurview = ds.Tables[0].Rows[0]["ArtEditPurview"].ToString();
              
                if (ds.Tables[0].Rows[0]["ShowOrder"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["ShowOrder"].ToString() == "1") || (ds.Tables[0].Rows[0]["ShowOrder"].ToString().ToLower() == "true"))
                    {
                        model.ShowOrder = true;
                    }
                    else
                    {
                        model.ShowOrder = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsVersion"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsVersion"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsVersion"].ToString().ToLower() == "true"))
                    {
                        model.IsVersion = true;
                    }
                    else
                    {
                        model.IsVersion = false;
                    }
                }
                model.IsTop = ds.Tables[0].Rows[0]["IsTop"].ToString();
                if (ds.Tables[0].Rows[0]["PublishStatus"].ToString() != "")
                {
                    model.PublishStatus = int.Parse(ds.Tables[0].Rows[0]["PublishStatus"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ArtId,TypeId,BorderId,ParentArtId,ArtOrder,ArtDirectory,ArtTopic,ArtKeys,ArtFrom,ArtSummary,ArtFileName,ModfiyTime,hits,Modify,Writer,Auditer,Audit,ArtFormat,Version,CanView,ArtViewPurview,ArtEditPurview,ShowOrder,IsVersion,IsTop,PublishStatus ");
            strSql.Append(" FROM cmsArticle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ArtId,TypeId,BorderId,ParentArtId,ArtOrder,ArtDirectory,ArtTopic,ArtKeys,ArtFrom,ArtSummary,ArtFileName,ModfiyTime,hits,Modify,Writer,Auditer,Audit,ArtFormat,Version,CanView,ArtViewPurview,ArtEditPurview,ShowOrder,IsVersion,IsTop,PublishStatus ");
            strSql.Append(" FROM cmsArticle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */
        public int AddVersion(int newid, int oldid)
        {
            List<string> l = new List<string>();
            l.Add("Update cmsArticle set IsVersion='1',ParentArtId=" + newid + " where ArtId=" + oldid);
            l.Add("Update cmsArticle set ParentArtId=" + newid + " where ParentArtId=" + oldid);
            l.Add("Update cmsReview set ArtId=" + newid + " where ArtId=" + oldid);
            return DbHelperSQL.ExecuteSqlTran(l);
        }

        public void UpHits(int id)
        {
            string sql = "update cmsArticle set hits=hits+1 where ArtId=" + id;
            DbHelperSQL.ExecuteSql(sql);
        }

        public int ChangeOrder(int up, int down)
        {
            return DbHelperSQL.ExecuteSql("exec Cms_TranArtOrder " + up + " , " + down);
        }
        #endregion  成员方法
    }
}

